import addMusicModal from './add-music-modal.vue';

export default {
    // name:'music-index',
    components: {
        addMusicModal
    },
    computed: {
        user() {
            return this.$store.state.user.data
        }
    },
    data() {
        return {
            text: '',
            showMusicMenu: true,
            musicTypeList: [],
            selectMenu: [],
            isOnline: false,
            showAddModal: false,
            musicList: '',
            pageLoading: false,
            pageNum: 1,
            pageCount: 0,
            searchKey: '',
            audio_type: '',
            openIndex: '',
            injectData: {},
            modalTreeData: [],
            ordering:false
        }
    },
    methods: {
        hideMusicMenu() {
            this.showMusicMenu = !this.showMusicMenu;
        },
        /**
         * 获取音乐类型列表
         *
         */
        initGetMusicTypeList() {

            this.http.post(this.postUrl.getMusicType, {
                token: this.user.tokenList[0],
                app_source: 'xpqh'
            }).then((data) => {
                console.log(data);
                if (data.code > -1) {
                    //以下是音乐目录数据处理
                    let parentTypeList = [];
                    let childTypeList = [];
                    data.data.forEach(item => {
                        if (item.depth == 1) {
                            parentTypeList.push(item)
                        } else {
                            childTypeList.push(item)
                        }
                    });
                    console.log(parentTypeList, childTypeList);
                    parentTypeList.forEach(parent => {
                        parent.title = parent.type_name + (parent.is_album == '1' ? ' (专辑)' : '')
                        childTypeList.forEach(child => {
                            child.title = child.type_name + (child.is_album == '1' ? ' (专辑)' : '')
                            if (child.parent_uuid == parent.uuid) {
                                if (!parent.children) {
                                    parent.children = []
                                }
                                parent.children.push(child)
                            }

                        });
                    });
                    parentTypeList.forEach(parent => {
                        if (parent.children && parent.children.length) {
                            parent.children.sort((a, b) => {
                                return a.orders - b.orders
                            })

                        }
                    });
                    parentTypeList.sort((a, b) => {
                        return a.orders - b.orders
                    })
                    let root = {
                        title: '全部',
                        expand: true,
                        children: parentTypeList,
                        uuid: 'ALL',
                        selected: true
                    }
                    this.selectMenu.push({
                        title: '全部'
                    })
                    parentTypeList.forEach(parent => {
                        if (parent.children && parent.children.length > 0) {
                            parent.disableCheckbox = true;
                        }
                    });
                    this.modalTreeData = JSON.parse(JSON.stringify(parentTypeList));
                    this.musicTypeList.push(root);
                    console.log(parentTypeList);
                } else {
                    this.errorHandle(data.msg)
                }
            }, () => {
                this.errorHandle()
            })
        },
        /**
         * 获取音乐
         */
        getMusicList() {
            if (this.pageLoading) return
            this.pageLoading = true;
            this.http.post(this.postUrl.getMusicList, {
                token: this.user.tokenList[0],
                pageNum: this.pageNum,
                pageSize: 20,
                key_word: this.searchKey,
                audio_type_uuid: this.audio_type.uuid ? this.audio_type.uuid : 'ALL',
                is_leaf: this.audio_type.leaf_flag == '1' ? 1 : 0
            }).then((data) => {
                console.log(data);

                if (data.code > -1) {
                    this.$nextTick(() => {
                        this.pageCount = data.data.totalRow;
                    })
                    this.musicList = JSON.parse(JSON.stringify(data.data.list))
                } else {
                    this.errorHandle(data.msg)
                }
                this.pageLoading = false;
            }, () => {
                this.pageLoading = false;
                this.errorHandle()
            })
        },
        /**
         * 点击右边音乐类型触发
         * @param {any} e
         */
        onSelectChange(e) {

            let selected = e[0];
            console.log(selected);

            if (selected.depth == 1) {
                this.$set(this.selectMenu, 0, selected)
                if (this.selectMenu[1]) {
                    this.selectMenu.splice(1, 1)
                }

                // this.selectMenu[0] = selected;
            } else if (selected.depth == 2) {
                this.modalTreeData.forEach(parent => {
                    if (selected.parent_uuid == parent.uuid) {
                        this.$set(this.selectMenu, 0, parent)
                    }
                });
                this.$set(this.selectMenu, 1, selected)
            } else if (selected.uuid == 'ALL') {
                this.$set(this.selectMenu, 0, selected)
                if (this.selectMenu[1]) {
                    this.selectMenu.splice(1, 1)
                }

            }
            this.audio_type = selected;
            this.getMusicList();
        },
        isOnlineChange(e, music, index) {
            console.log(e, music, index);
            this.musicList[index].is_online = '1'

            this.http.post(this.postUrl.musicIsOnlineUrl, {
                token: this.user.tokenList[0],
                audio_uuid: music.uuid,
                is_online: e ? '1' : '0'
            }).then((data) => {
                if (data.code < 0) {
                    this.musicList[index].is_online = '0'
                }
            }, () => {
                this.musicList[index].is_online = '0'

            })
        },
        changePage(num) {
            this.pageNum = num;
            this.getMusicList();
            document.querySelector('.single-page-con').scrollTo(0, 0)
        },
        /**
         * 展开详细信息
         * @param {Object} music
         * @param {Number} index
         */
        showDetail(music, index) {
            if (this.openIndex !== '' && this.openIndex !== index) {
                this.$set(this.musicList[this.openIndex], 'showDetail', false)
            }
            this.openIndex = index;
            this.$set(this.musicList[index], 'showDetail', this.musicList[index].showDetail ? false : true)
        },
        /**
         * 对音质字符串进行处理
         * @param {String} text
         */
        qualityFormat(text) {
            if (text.indexOf(',') > -1) {
                let textArr = text.split(',');
                let newtextArr = textArr.map(item => {
                    return item.replace('音质', '')
                })
                return newtextArr.join('/')
            } else {
                return text.replace('音质', '')
            }
        },
        clearSearch() {
            this.searchKey = '';
            this.pageNum = 1;
            this.getMusicList();
        },
        doSearch() {

            this.pageNum = 1;
            this.getMusicList();

        },
        editMusic(music) {
            music.edit = true;
            this.injectData = music;
            this.showAddModal = true;
        },
        addMusicSuccess() {
            this.getMusicList();
        },
        deleteMusic(music, index) {
            this.$Modal.confirm({
                title: '注意',
                content: '<p>删除后将无法恢复，是否删除？</p>',
                loading: true,
                onOk: () => {
                    this.http.post(this.postUrl.deleteMusicUrl, {
                        token: this.user.tokenList[0],
                        audio_uuid: music.uuid
                    }).then((data) => {
                        if (data.code > -1) {
                            this.musicList.splice(index, 1)
                            this.$Message.success('删除成功!');
                        } else {
                            this.$Message.error('删除失败！');

                        }
                    }, () => {
                        this.$Message.error('删除失败！');
                    }).then(() => {
                        this.$Modal.remove();
                    })
                }
            });
        },
        orderMusic(music,move_up_down){
            if(this.ordering) return
            this.ordering = true;
            this.http.post(this.postUrl.doOrderMusicUrl,{
                token:this.user.tokenList[0],
                move_uuid:music.uuid,
                move_up_down:move_up_down,
                vod_audio_type_uuid:this.audio_type.uuid
            }).then((data)=>{
                if(data.code > -1){
                    this.getMusicList()
                }else{
                    this.errorHandle(data.msg)
                }
            },()=>{
                this.errorHandle()

            }).then(()=>{
                this.ordering = false;
            })
        }
    },
    created() {
        setTimeout(() => {
            this.initGetMusicTypeList()
            this.getMusicList();
        }, 200);
    },
    watch: {
        showAddModal(val) {
            if (!val) {
                this.injectData = {}
            }
        }
    }
}
